package com.chenille.tools.http

import android.content.Context
import androidx.annotation.NonNull
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import okhttp3.OkHttpClient
import java.io.InputStream

/**
 * @Author: FALLS CHEN
 * @Date: 2020-01-20 17:40
 * EMAIL: 636e68747474403136332e636f6d
 * @Version 1.0
 */
@GlideModule
class OkHttpAppGlideModule :AppGlideModule(){

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        val cacheSize100MegaBytes = 104857600
        builder.setDiskCache(
            InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes.toLong())
        )

    }

    override fun registerComponents(@NonNull context: Context, @NonNull glide: Glide, @NonNull registry: Registry) {
        registry.replace(
            GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(
                OkHttpClient()
            ))
    }
}